<?php

namespace app\admin\controller;

use app\common\library\Openssl;
use think\db\Query;
use think\facade\Config;
use think\Db;
use think\helper\Time;
use think\facade\Cache;

class Index extends Base
{
    protected $user = 0;

    protected $beforeActionList = [
        'first'
    ];

    protected function first()
    {
        $this->user = session('uid');
    }

    public function groups()
    {
        $qrcode=$this->qrcode();
        $this->assign('wxqrcode', $qrcode);
        return view('groups');
    }

    public function qrcode()
    {

        $openssl = new Openssl();
        $data['expires_in'] = time() + 60 * 10;
        $data['id'] = substr(config('cache.prefix'), 1);
        $token = $openssl->encrypt(json_encode($data));
        $url = 'https://openwx.weibuyun.com/auth/wxqrcode?token=' . $token;
        return @file_get_contents($url);
    }

    public function project_admin()
    {
        $map = array();
        $data = array();
        $map[] = ['group_id', '=', 11];
        $users = Db::view('admin', 'id,nickname')
            ->view('auth_group_access', 'group_id', 'admin.id=auth_group_access.uid')
            ->where($map)
            // ->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();
        $ca_cache = 600;
        $this->assign('userlist', $users);
        $data['project_num'] = DB::name('project')->cache($ca_cache)->count();
        $data['model1_num'] = DB::name('stories')->where('model_id', 1)->cache($ca_cache)->count();
        $data['model2_num'] = DB::name('stories')->where('model_id', 2)->cache($ca_cache)->count();
        $data['model3_num'] = DB::name('stories')->where('model_id', 3)->cache($ca_cache)->count();
        $data['doc_num'] = DB::name('doc')->cache($ca_cache)->count();
        $data['doccomment_num'] = DB::name('doccomment')->cache($ca_cache)->count();
        $data['comment_num'] = DB::name('comment')->cache($ca_cache)->count();
        $data['dev_num'] = DB::name('auth_group_access')->where('group_id', 11)->cache($ca_cache)->count();
        $data['jia_num'] = DB::name('auth_group_access')->where('group_id', 12)->cache($ca_cache)->count();
        $wx = DB::view('wx_msg_info', 'msgid')
            ->view('wx_msg', 'c_time', 'wx_msg_info.msg_id=wx_msg.id')
            ->where('msgid', '>', 0)
            ->order('c_time desc')
            ->cache($ca_cache)
            ->find();
        //dump($wx);
        $data['wx_time'] = date('Y-m-d H:i:s', $wx['c_time']);
        //dump($data);
        $this->assign('sof', $data);

        list($map['s_time'], $map['e_time']) = Time::today();
        $login = DB::name('workreg_d')->where('uid', session('uid'))->where('d_time', $map['s_time'])->find();
        // dump($login);
        if ($login) {
            $login['am_in'] = $login['am_in'] ? date('h:i', $login['am_in']) : null;
            $login['am_out'] = $login['am_out'] ? date('h:i', $login['am_out']) : null;
            $login['fm_in'] = $login['fm_in'] ? date('h:i', $login['fm_in']) : null;
            $login['fm_out'] = $login['fm_out'] ? date('h:i', $login['fm_out']) : null;
            //dump($login);
            // exit;

            $this->assign('login', $login);
        } else {
            $data2['uid'] = session('uid');
            $data2['d_time'] = $map['s_time'];
            $login = DB::name('workreg_d')->insert($data2);
        }


        return view('project_admin');
    }

    public function treeproject()
    {
        $list = [];
        $list[0]['title'] = '预备项目';
        $list[0]['id'] = '预备项目';
        $list[0]['spread'] = 'true';
        $list[1]['title'] = '进行中项目';
        $list[1]['id'] = '进行中项目';
        $list[1]['spread'] = 'true';
        $list[2]['title'] = '维护项目';
        $list[2]['id'] = '维护项目';
        $list[2]['spread'] = 'true';
        $list[3]['title'] = '其他';
        $list[3]['id'] = '其他';
        $list[3]['spread'] = 'true';
        $map = [];
        $group = DB::name('auth_group_access')->where('uid', $this->user)->select();
        $group_ids = array_column($group, 'group_id');
        //dump($group_ids);
        //判断当前用户身份
        if (in_array(12, $group_ids)) {
            $map[] = ['uid', '=', $this->user];
            $p_list = DB::view('project', 'id,title')
                ->view('project_user', 'uid', 'project.id=project_user.project_id', 'LEFT')
                ->where($map)
                ->where('status', 'in', '1,2,3,4')
                ->select();
            foreach ($p_list as $key => $item) {
                $list[0]['children'][$key] = $item;
            }
            $p_list = DB::view('project', 'id,title')
                ->view('project_user', 'uid', 'project.id=project_user.project_id', 'LEFT')
                ->where($map)
                ->where('status', 'in', '5,6,7,8')->select();
            foreach ($p_list as $key => $item) {
                $list[1]['children'][$key] = $item;
            }
            $p_list = DB::view('project', 'id,title')
                ->view('project_user', 'uid', 'project.id=project_user.project_id', 'LEFT')
                ->where($map)
                ->where('status', 'in', '9')->select();
            foreach ($p_list as $key => $item) {
                $list[2]['children'][$key] = $item;
            }
            $p_list = DB::view('project', 'id,title')
                ->view('project_user', 'uid', 'project.id=project_user.project_id', 'LEFT')
                ->where($map)
                ->where('status', 'in', '10')->select();
        } else {
            $p_list = DB::view('project', 'id,title')
                ->where('status', 'in', '1,2,3,4')
                ->select();
            foreach ($p_list as $key => $item) {
                $list[0]['children'][$key] = $item;
            }
            $p_list = DB::view('project', 'id,title')
                ->where('status', 'in', '5,6,7,8')->select();
            foreach ($p_list as $key => $item) {
                $list[1]['children'][$key] = $item;
            }
            $p_list = DB::view('project', 'id,title')
                ->where('status', 'in', '9')->select();
            foreach ($p_list as $key => $item) {
                $list[2]['children'][$key] = $item;
            }
            $p_list = DB::view('project', 'id,title')
                ->where('status', 'in', '10')->select();
        }

        foreach ($p_list as $key => $item) {
            $list[3]['children'][$key] = $item;
        }
        $data['data'] = $list;
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function add_no_time()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = new \app\admin\validate\Notime();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            Db::startTrans();
            try {
                $day = explode(' - ', $post['s_time']);
                $s_time = strtotime($day[0]);
                $e_time = strtotime($day[1]);
                $day_num = round($e_time - $s_time) / 86400 + 1;
                $data = [];
                for ($i = 1; $i <= $day_num; $i++) {
                    $item = [];
                    $item['d_time'] = $s_time + 86400 * ($i - 1);
                    $item['info'] = $post['info'];
                    $item['status'] = $post['no_time'];
                    $item['uid'] = session('uid');
                    $data[] = $item;
                }
                DB::name('workreg_d')->insertAll($data);

                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        } else {
            $this->assign('no_time', config('param.no_time'));
            return view();
        }
    }

    public function savelogin()
    {
        list($map['s_time'], $map['e_time']) = Time::today();

        $info = DB::name('workreg_d')->where('uid', session('uid'))->where('d_time', $map['s_time'])->find();
        if ($info) {
            if ($info[input('data')] == 0) {
                $info = DB::name('workreg_d')->where('uid', session('uid'))->where('d_time', $map['s_time'])->update([input('data') => time(), 'status' => 4]);
                $msg = '';
                switch (input('data')) {
                    case 'am_in':
                        $msg = '上午签到';
                        break;
                    case 'am_out':
                        $msg = '上午签退';
                        break;
                    case 'fm_in':
                        $msg = '下午签到';
                        break;
                    case 'fm_out':
                        $msg = '下午签退';
                        break;
                    default:
                        $msg = '上午签到';
                }
                $data['data'] = $msg . date('h:i', time());
                $data['msg'] = '提交成功';
                $data['code'] = 1;
            } else {
                $data['msg'] = '已经做过操作,不能重复操作';
                $data['code'] = 0;
            }

        } else {
            $data['msg'] = '提交失败';
            $data['code'] = 0;

        }
        return json($data);

    }

    public function getuserfinish()
    {
        //七天新增
        if (input('uid')) {
            $uid = input('uid');
        } else {
            $info = Db::view('admin', 'id,nickname')
                ->view('auth_group_access', 'group_id', 'admin.id=auth_group_access.uid')
                ->field('id,nickname')
                ->where('group_id', 11)
                ->find();
            $uid = $info['id'];
        }
        $sm = Time::daysAgo(90);
        $data['data']['sm'] = date('Y-m-1', $sm);
        list($sm, $em) = Time::Month();
        $data['data']['em'] = date('Y-m-d', $em);
        $userinfo = Db::view('admin', 'id,nickname')
            ->view('auth_group_access', 'group_id', 'admin.id=auth_group_access.uid')
            ->field('id,nickname')
            ->where('admin.id', $uid)
            ->find();
        $data['data']['nickname'] = $userinfo['nickname'];
        $list = DB::name('work_user_time')->where('uid', $uid)->where('c_time', '>', $sm)->field('c_time,w_time')->select();
        foreach ($list as &$item) {
            $item['c_time'] = date('Y-m-d', $item['c_time']);
            $item['w_time'] = $item['w_time'];
        }
        $data['data']['data'] = $list;
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);

    }

    public function getprojectbar()
    {
        //项目信息图
        $project_id = input('project_id');
        $data = [];
        $map = [];
        $group = DB::name('auth_group_access')->where('uid', $this->user)->select();
        $group_ids = array_column($group, 'group_id');
        //dump($group_ids);
        //判断当前用户身份
        if (in_array(12, $group_ids)) {
            $map[] = ['uid', '=', $this->user];
            $project = DB::view('project', 'title,id')
                ->view('project_user', 'uid', 'project.id=project_user.project_id')
                ->where($map)
                ->where('project.id', $project_id)
                ->strict(true)->find();
            //dump($project);
            if (!$project) {
                $project_info = DB::view('project', 'title,id')
                    ->view('project_user', 'uid', 'project.id=project_user.project_id')
                    ->where($map)
                    ->find();
                $project_id = $project_info['id'];
                $project = DB::name('stories')->where('project_id', $project_id)->strict(true)->find();
            }
        } else {
            $project = DB::name('project')
                ->where('id', $project_id)
                ->strict(true)->find();
            //dump($project);
            if (!$project) {
                $project_info = DB::name('project')->strict(true)->find();
                $project_id = $project_info['id'];
                $project = DB::name('stories')->where('project_id', $project_id)->strict(true)->find();
            }
        }


        $data['project_name'] = DB::name('project')->where('id', $project_id)->value('title');
        $ndata['data'] = [];
        //得出项目各内容值
        $data['m_1']['sum'] = DB::name('stories')->where('project_id', $project_id)->where('model_id', 1)->count();
        $data['m_1']['yes'] = DB::name('stories')->where('project_id', $project_id)->where('model_id', 1)->where('status', 5)->count();
        $data['m_1']['no'] = $data['m_1']['sum'] - $data['m_1']['yes'];
        $data['m_2']['sum'] = DB::name('stories')->where('project_id', $project_id)->where('model_id', 2)->count();
        $data['m_2']['yes'] = DB::name('stories')->where('project_id', $project_id)->where('model_id', 2)->where('status', 5)->count();
        $data['m_2']['no'] = $data['m_2']['sum'] - $data['m_2']['yes'];
        $data['m_3']['sum'] = DB::name('stories')->where('project_id', $project_id)->where('model_id', 3)->count();
        $data['m_3']['yes'] = DB::name('stories')->where('project_id', $project_id)->where('model_id', 3)->where('status', 5)->count();
        $data['m_3']['no'] = $data['m_3']['sum'] - $data['m_3']['yes'];
        if ($data['m_1']['sum']) {
            $data['m_1']['p'] = round($data['m_1']['yes'] / $data['m_1']['sum'] * 100, 2);
        } else {
            $data['m_1']['p'] = 0;
        }
        if ($data['m_2']['sum']) {
            $data['m_2']['p'] = round($data['m_2']['yes'] / $data['m_2']['sum'] * 100, 2);
        } else {
            $data['m_2']['p'] = 0;
        }
        if ($data['m_3']['sum']) {
            $data['m_3']['p'] = round($data['m_3']['yes'] / $data['m_3']['sum'] * 100, 2);
        } else {
            $data['m_3']['p'] = 0;
        }
        $ndata['data'] = $data;

        if ($ndata['data']) {
            $ndata['msg'] = '获取成功';
        } else {
            $ndata['msg'] = '获取数据为空';
        }
        $ndata['code'] = 0;
        return json($ndata);
    }

    public function index()
    {
        $group = DB::name('auth_group_access')->where('uid', $this->user)->select();
        $group_ids = array_column($group, 'group_id');

        //dump($group_ids);
        //判断当前用户身份
        if (in_array(12, $group_ids)) {
            //甲方人员
            $this->redirect('groups');
        } else if (in_array(13, $group_ids) || in_array(1, $group_ids)) {
            //管理员

            // exit;
            $this->redirect('project_admin');
        } else {
            //开发人员
            list($map['s_time'], $map['e_time']) = Time::today();
            $login = DB::name('workreg_d')->where('uid', session('uid'))->where('d_time', $map['s_time'])->find();
            // dump($login);
            if ($login) {
                $login['am_in'] = $login['am_in'] ? date('h:i', $login['am_in']) : null;
                $login['am_out'] = $login['am_out'] ? date('h:i', $login['am_out']) : null;
                $login['fm_in'] = $login['fm_in'] ? date('h:i', $login['fm_in']) : null;
                $login['fm_out'] = $login['fm_out'] ? date('h:i', $login['fm_out']) : null;
                //dump($login);
                // exit;
                $this->assign('login', $login);
            } else {
                $data['uid'] = session('uid');
                $data['d_time'] = $map['s_time'];
                $login = DB::name('workreg_d')->insert($data);
            }
            $this->assign('qrcode', $this->qrcode());
            $qrcode=$this->qrcode();
            $this->assign('wxqrcode', $qrcode);
            return view('index');
        }

    }

    public function work_user_time()
    {
        //用户工作时间
        $map['uid'] = $this->user;
        $count = Db::name('work_user_time')->where($map['uid'])->count();
        if (input('firstpage') == 1) {
            if ($count > 10) {
                $limit = 10;
            } else {
                $limit = $count;
            }
        } else {
            $limit = 1;
        }
        if (input('page') > $count && $count > 0) {
            $page = input('page') % $count;
        } else {
            $page = input('page');
        }
        $list = Db::name('work_user_time')->where($map['uid'])->page($page, $limit)->cache(300)->select();
        foreach ($list as &$item) {
            $item['c_time'] = $item['c_time'] = date('m-d', $item['c_time']);
        }
        $data['data'] = $list;
        $data['msg'] = '获取成功';
        $data['code'] = 0;
        return json($data);
    }

    public function getcalendar()
    {
        //日程
        $post = $this->request->param();
        $data = [];
        $group = DB::name('auth_group_access')->where('uid', $this->user)->select();
        $group_ids = array_column($group, 'group_id');
        //dump($group_ids);
        //判断当前用户身份
        if (in_array(13, $group_ids) || in_array(1, $group_ids)) {
            if (input('uid')) {
                $uid = input('uid');
                $data['nickname'] = Db::name('admin')
                    ->where('id', input('uid'))
                    ->value('nickname');
            } else {
                $info = Db::view('admin', 'id,nickname')
                    ->view('auth_group_access', 'group_id', 'admin.id=auth_group_access.uid')
                    ->field('id,nickname')
                    ->where('group_id', 11)
                    ->find();
                $data['nickname'] = $info['nickname'];
                $uid = $info['id'];
            }
        } else {
            $uid = $this->user;
        }
//        $list = DB::name('stories')
//            ->where('touid', $uid)
//            ->whereTime('s_time', '>=', $post['start'])
//            ->whereTime('e_time', '<=', $post['end'])
//            ->where('delete_time',0)
//            ->field('id,title,s_time,e_time,model_id,status')
//            ->select();
        $list = DB::view('stories', 's_time,e_time,delete_time,id,title,model_id,status')
            ->view('project', ['title' => 'project_title'], 'stories.project_id=project.id')
            ->where('touid', $uid)
            ->whereTime('stories.s_time', '>=', $post['start'])
            ->whereTime('stories.e_time', '<=', $post['end'])
            ->where('stories.delete_time', 0)
            ->select();
        $project_model = config('param.project_model');
        foreach ($list as &$info) {
            $info['title'] = $info['project_title'] . '[' . $project_model[$info['model_id']] . ']' . $info['title'];
            $info['start'] = date('Y-m-d', $info['s_time']);
            $info['end'] = date('Y-m-d', $info['e_time']);
        }
        $data['data'] = $list;
        $data['msg'] = '获取成功';
        $data['code'] = 0;
        return json($data);
    }

    public function getfinish()
    {
        list($map['s_time'], $map['e_time']) = Time::month();
        $info['f']['sum'] = Db::name('stories')
            ->where('s_time', '>=', $map['s_time'])
            ->where('s_time', '<=', $map['e_time'])
            ->where('uid', $this->user)
            ->count();
        $info['f']['ok'] = Db::name('stories')
            ->where('s_time', '>=', $map['s_time'])
            ->where('s_time', '<=', $map['e_time'])
            ->where('status', '=', 5)
            ->where('uid', $this->user)
            ->count();
        $info['f']['n'] = $info['f']['sum'] - $info['f']['ok'];
        $info['p']['sum'] = Db::name('stories')
            ->where('s_time', '>=', $map['s_time'])
            ->where('s_time', '<=', $map['e_time'])
            ->count();
        $info['p']['ok'] = Db::name('stories')
            ->where('s_time', '>=', $map['s_time'])
            ->where('s_time', '<=', $map['e_time'])
            ->where('status', '=', 4)
            ->where('uid', $this->user)
            ->count();
        $info['p']['n'] = $info['f']['sum'] - $info['f']['ok'];
        //月效率
        $list = DB::name('user_work')->where('uid', $this->user)->page(1, 6)->select();
        foreach ($list as &$item) {
            $item['m_time'] = date('y-m', $item['m_time']);

        }
        $info['mwork'] = $list;
        $data['data'] = $info;
        $data['msg'] = '获取成功';
        $data['code'] = 0;
        return json($data);
    }

    public function festivallists()
    {
        //放假日期
        $map = array();
        $data['count'] = Db::name('festival')
            ->where('d_time', '>=', time())
            ->count();
        $data['data'] = Db::name('festival')
            ->where('d_time', '>=', time())
            // ->fetchSql(true)
            ->order('d_time asc')->page(input("param.page"), input('param.limit'))->select();
        foreach ($data['data'] as &$info) {
            $info['d_time'] = date('Y-m-d', $info['d_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function storieslists()
    {
        $map = array();

        $map[] = ['stories.uid|stories.touid', '=', $this->user];
        $map[] = ['stories.delete_time', '=', 0];
        $map[] = ['stories.status', '<>', 5];
        $data['data'] = Db::view('stories', 'id,project_id,title,touid,e_time,uid,p_time,status,delete_time,model_id')
            ->view('project', ['title' => 'project_title'], 'stories.project_id=project.id')
            ->where($map)
            // ->fetchSql(true)
            ->order('e_time asc')
            ->page(1, 15)
            ->select();
        $project_model = config('param.project_model');
        foreach ($data['data'] as &$info) {
            $info['title'] = '[' . $info['project_title'] . ']' . $info['title'];
            $info['e_time'] = date('m-d', $info['e_time']);
            $info['model_id'] = $project_model[$info['model_id']];
        }
        $data['count'] = 15;
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


    //更新侧边栏状态
    public function flexible()
    {
        $menu = $this->request->param('menu', 'open', 'trim');
        session('menu_status', $menu);
    }
}
